package com.bilibili.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bilibili.dao.BaseDao;
import com.bilibili.dao.WatchHistoryDao;
import com.bilibili.entity.Video;
import com.bilibili.entity.WatchHistory;

public class WatchHistoryDaoImpl implements WatchHistoryDao {

	@Override
	public List<Video> selectByUpId(int uploaderId) {
		Connection conn = BaseDao.getConn();// 打开连接
		String sql = "select video.* FROM watch_history w1 JOIN video WHERE video.video_id=w1.video_id AND user_id="+uploaderId+" order by w1.history_id desc";
		List<Video> list = new ArrayList<Video>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				Video v = new Video();
				v.setVideoId(rs.getInt("video_id"));
				v.setTitle(rs.getString("title"));
				v.setDescription(rs.getString("description"));
				v.setCoverUrl(rs.getString("cover_url"));
				v.setVideoUrl(rs.getString("video_url"));
				v.setCategoryId(rs.getInt("category_id"));
				v.setUploaderId(rs.getInt("uploader_id"));
				v.setCreateTime(rs.getDate("create_time"));
				v.setDuration(rs.getInt("duration"));
				v.setStatus(rs.getInt("status"));
				v.setViewCount(rs.getInt("view_count"));
				v.setLikeCount(rs.getInt("like_count"));
				v.setCoinCount(rs.getInt("coin_count"));
				v.setCollectCount(rs.getInt("collect_count"));
				list.add(v);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(conn, ps, rs);
		}
		return list;
	}

	@Override
	public WatchHistory selectWatchHistory(WatchHistory w) {
		// TODO Auto-generated method stub
		Connection conn = BaseDao.getConn();// 打开连接
		String sql = "select * FROM watch_history where user_id = ? AND video_id = ? ";
		WatchHistory wh = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, w.getUserId());
			ps.setInt(2, w.getVideoId());
			rs = ps.executeQuery();
			while (rs.next()) {
			    wh = new WatchHistory();
			    wh.setHistoryId(rs.getInt("history_id"));
			    wh.setUserId(rs.getInt("user_id"));
			    wh.setVideoId(rs.getInt("video_id"));
			    wh.setWatchTime(rs.getDate("watch_time"));
			    wh.setDuration(rs.getInt("duration"));
			    wh.setProgress(rs.getInt("progress"));
			    wh.setIsCompleted(rs.getInt("is_completed"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(conn, ps, rs);
		}
		return wh;
	}

	@Override
	public int deleteWatchHistory(int wId) {
		// TODO Auto-generated method stub
		String sql = "delete from watch_history where history_id = ?";
		Object[] param = {wId};
		return BaseDao.executeUpdate(sql, param);
	}

	@Override
	public int insertWatchHistory(WatchHistory w) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO watch_history VALUE(default,?,?,NOW(),default,default,default)";
		Object[] param = {w.getUserId(),w.getVideoId()};
		return BaseDao.executeUpdate(sql, param);
	}

}
